package com.auqaoasis.model.table;

import com.auqaoasis.exception.DbException;
import com.auqaoasis.model.row.Row;
import com.auqaoasis.model.table.tableFile.TableFile;

import java.io.IOException;
import java.util.Objects;

/**
 * @Author Auqaoasis
 * @Date 2022/3/30 - 13:23
 * @Description: 表对象
 */
public class Table {
    /**
     * 表名
     */
    private String tableName;

    /**
     * table id
     */
    private int tableId;

    /**
     * 表文件
     */
    private TableFile tableFile;

    /**
     * 表结构
     */
    private TableDesc tableDesc;

    public Table(String tableName, TableFile tableFile, TableDesc tableDesc) {
        this.tableName = tableName;
        this.tableId = tableFile.getTableId();
        this.tableFile = tableFile;
        this.tableDesc = tableDesc;
    }

    public TableDesc getTableDesc() {
        return this.tableDesc;
    }

    public String getTableName() {
        return tableName;
    }

    public TableFile getHeapTableFile() {
        return tableFile;
    }

    public int getTableId() {
        return tableId;
    }

    public void insertRow(Row row) throws IOException {
        if (!Objects.equals(row.getTableDesc(), this.getTableDesc())) {
            throw new DbException("insertRow error: TableDesc not match");
        }
        this.tableFile.insertRow(row);
    }
}
